package com.dgd.test.demo.controller;

import com.dgd.test.demo.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.*;


/**
 * @program: 726
 * @description:
 * @author: Jaime
 * @create: 2021-07-26 12:35
 **/
@Controller
public class HelloControll {
    @Autowired
    private HelloService helloService;

    @ResponseBody
    @RequestMapping("/hello")
    public void show(){
        System.out.println("抓到了");
        helloService.addTeacher();
    }

    //注释 测试图片下载功能
    @RequestMapping("/edu/download")
    public void download(String fileName, HttpServletResponse response) throws IOException {

        response.setContentType("application/force-download");
        response.addHeader("Content-Disposition", "attachment;fileName=" + fileName);
        ServletOutputStream outputStream =  response.getOutputStream();
        BufferedOutputStream bos = new BufferedOutputStream(outputStream);
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream("E:\\vsWorkSpace\\edu-manager\\img\\result.png"));
        int len= 0;
        byte[] b = new byte[1024];

        while ((len = bis.read(b) ) !=-1 ){
            bos.write(b,0,len);
            bos.flush();
        }
        bis.close();
        bos.close();
    }
}
